package com.grsu.TransportSystem.controllers;

import java.security.Principal;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.grsu.TransportSystem.constants.ProposalStates;
import com.grsu.TransportSystem.constants.Roles;
import com.grsu.TransportSystem.domain.Person;
import com.grsu.TransportSystem.domain.Proposal;
import com.grsu.TransportSystem.services.PersonService;
import com.grsu.TransportSystem.services.ProposalService;

@Controller
public class TripController {

	@Autowired
	ProposalService proposalService;
	
	@Autowired
	PersonService personService;
	
	@RequestMapping(value = "/getAllTrips")
	public String getAllTrips(HttpServletRequest request) {
		String resultjsp = "ADMINGetTrips";
		List<Proposal> result = proposalService.getProposal(new Proposal());
		request.setAttribute("trips", result);
		return resultjsp;
	}
	
	@RequestMapping(value = "/getMyTrips")
	public String getMyTrips(HttpServletRequest request , HttpSession session ) {
		String resultjsp = "DRIVERGetTrips";
	    Person user = (Person)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String userId = user.getId();
		Person driver = personService.getPersonById(userId);
		Proposal temp = new Proposal();
		temp.setDriver(driver);
		List<Proposal> result = proposalService.getProposalByDriver(driver);
		request.setAttribute("trips", result);
		return resultjsp;
	}
	
	@RequestMapping(value = "/deleteTrip")
	public String deleteTrip(HttpServletRequest request, @ModelAttribute(value = "proposal") Proposal proposal) {
		String resultjsp = "ADMINGetTrips";
		proposalService.deleteProposal(proposal);
		List<Proposal> result = proposalService.getProposal(new Proposal());
		request.setAttribute("trips", result);
		return resultjsp;
	}
	
	@RequestMapping(value = "/getTripsVsDrivers")
	public String getTripsVsDrivers(HttpServletRequest request) {
		String resultjsp = "ADMINGetTripsAndDrivers";
		List<Proposal> resultTrip = proposalService.getProposal(new Proposal());
		request.setAttribute("trips", resultTrip);
		List<Person> resultPerson = personService.getPersonByRole(Roles.DRIVER);
		request.setAttribute("drivers", resultPerson);
		return resultjsp;
	}
	
	@RequestMapping(value = "/updateDriverOfTrip")
	public String updateDriverOfTrip(HttpServletRequest request) {
		String tripId = (String) request.getParameter("tripId");
		String driverId = (String) request.getParameter("driverId");
		Proposal trip = proposalService.getProposalById(tripId);
		Person person = personService.getPersonById(driverId);
		trip.setDriver(person);
		trip.setState(ProposalStates.WAIT);
		proposalService.updateProposalDriver(trip);
		return getTripsVsDrivers(request);
	}
	
	@RequestMapping(value = "/createTrip")
	public String createTrip( @ModelAttribute(value = "proposal") Proposal proposal , HttpServletRequest request) {
		proposal.setState(ProposalStates.FREE);
		proposalService.createProposal(proposal);
		return getAllTrips( request);
	}
	
	@RequestMapping(value = "/getCreateTripForm")
	public String getCreateTripForm( ) {
		return "createTrip";
	}
	
	@RequestMapping(value = "/agreeWithTrip")
	public String agreeWithTrip( @ModelAttribute(value = "proposal") Proposal proposal , HttpServletRequest request ,HttpSession session) {
		String isAgree = request.getParameter("isAgree");
		if("true".equals(isAgree))
		{
			proposal.setState(ProposalStates.CONFIRM);
		}
		else
		{
			proposal.setState(ProposalStates.FREE);
		}
		proposalService.updateProposalState(proposal);
		return getMyTrips( request,session);
	}
	
}
